﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using OOAD_Project_Engine.Foundation;
using OOAD_Project_Engine.Domain.Entities;
using OOAD_Project_Engine.Application.Services;
using OOAD_Project_Engine.Domain.Components;
using System.Reflection;
using OOAD_Project_Engine.Foundation.Interfaces;
using OOAD_Project_Engine.Domain.Factories;

namespace OOAD_Project_Engine.Presentation
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class GameEngine : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics { get; set; }
        public static ConfigContent Config { get; private set; }
        SceneComponent scene;
        ContentProvider contentProvider;
        GraphicService graphicService;

        public GameEngine()
        {
            GameHelper.Game = this;
            graphics = new GraphicsDeviceManager(this);
            IsMouseVisible = true;
            Config = new ConfigContent();
            Content.RootDirectory = "Content";

            contentProvider = new ContentProvider(this);
            scene = new SceneComponent(this);
            graphicService = new GraphicService(this);
            Components.Add(graphicService);
            Components.Add(scene);
            Components.Add(contentProvider);

        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            base.Initialize();
            graphics.IsFullScreen = Config.IS_FULL_SCREEN;
            graphics.PreferredBackBufferHeight = Config.GAME_HEIGHT;
            graphics.PreferredBackBufferWidth = Config.GAME_WIDTH;
            graphics.ApplyChanges();
            Transform t = EntityComponentFactory.CreateComponent(typeof(Transform)) as Transform;
            t.Position = new Vector2(Config.GAME_WIDTH / 2, Config.GAME_HEIGHT / 2);
            //t.Origin = new Vector2(.5f);
            Sprite s;
            s = new Sprite("ele", true);
            //s = new Sprite("D:/meo/1319594217754.png", false);
            //s.Source = "ele";
            //s.LoadContent();
            s.Origin = new Vector2(.5f);

            Type type = Type.GetType(typeof(Entity).AssemblyQualifiedName);
            scene.Factory.Create(type, "test", s,t);

            //scene.Factory.Create<Entity>("test", t, s);

            //scene.Manager.Find("test").Add(s);

        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            //spriteBatch = new SpriteBatch(GraphicsDevice);
            ResourceManager.Initialize(Content);

        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
            ResourceManager.Dispose();
        }

        Vector2 scale = new Vector2(1);
        //float i = 0;

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();
            base.Update(gameTime);

            //Transform t = scene.Manager.Find("test").Find<Transform>();
            //t.Rotation += 0.05f;
            //t.Scale = t.Scale *= 1.05f;

        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            base.Draw(gameTime);

        }
    }
}
